package com.example.baserecyclerviewadapterdemo.activity;

import android.os.Bundle;
import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.adapter.diffUtil.DiffUtilAdapter;
import com.example.baserecyclerviewadapterdemo.data.DataServer;
import com.example.baserecyclerviewadapterdemo.entity.DiffUtilEntity;

import java.util.ArrayList;
import java.util.List;

public class DiffUtilActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private Button btnChange,btnPayload;
    private DiffUtilAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diffutil);

        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("ItemClickActivity");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = findViewById(R.id.diff_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        btnChange = findViewById(R.id.btnChange);
        btnPayload = findViewById(R.id.btnPayload);

        initRecyclerView();
        btnPayload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.getData().set(0,new DiffUtilEntity(1,"\uD83D\uDE0A\uD83D\uDE0AItem " + 0,
                        "市场分析公司 IDC 提供的数据显示，Chromebook 在 2020 年的市场份额获得大幅增长，首次突破 10%",
                        "21-03-01"));
                adapter.notifyItemChanged(0 + adapter.getHeaderLayoutCount(),DiffUtilAdapter.ITEM_0_PAYLOAD);
            }
        });
        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.setDiffNewData(getNewList());
            }
        });
    }

    private void initRecyclerView(){
        adapter = new DiffUtilAdapter(DataServer.getDiffUtilDemoEntities());
        View headerView = getLayoutInflater().inflate(R.layout.layout_head_view,recyclerView,false);
        headerView.findViewById(R.id.iv).setVisibility(View.GONE);
        adapter.addHeaderView(headerView);
        adapter.setAnimationEnable(true);
        recyclerView.setAdapter(adapter);
        adapter.setDiffCallback(new DiffUtil.ItemCallback<DiffUtilEntity>() {
            @Override
            public boolean areItemsTheSame(@NonNull DiffUtilEntity oldItem, @NonNull DiffUtilEntity newItem) {
                return oldItem.getId() == newItem.getId();
            }

            @Override
            public boolean areContentsTheSame(@NonNull DiffUtilEntity oldItem, @NonNull DiffUtilEntity newItem) {
                Log.i("newItem",newItem.getTitle());
                Log.i("oldItem",oldItem.getTitle());
                return oldItem.getTitle().equals(newItem.getTitle()) && oldItem.getContent().equals(newItem.getContent())
                        && oldItem.getDate().equals(newItem.getDate());
            }
        });

    }
    private List<DiffUtilEntity> getNewList() {
        List<DiffUtilEntity> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            /*
            Simulate deletion of data No. 1 and No. 3
            模拟删除1号和3号数据
             */
            if (i == 1 || i == 3) {
                continue;
            }

            /*
            Simulate modification title of data No. 0
            模拟修改0号数据的title
             */
            if (i == 0) {
                list.add(new DiffUtilEntity(
                        i,
                        "😊Item " + i,
                        "This item " + i + " content",
                        "06-12")
                );
                continue;
            }

            /*
            Simulate modification content of data No. 4
            模拟修改4号数据的content发生变化
             */
            if (i == 4) {
                list.add(new DiffUtilEntity(
                        i,
                        "Item " + i,
                        "Oh~~~~~~, Item " + i + " content have change",
                        "06-12")
                );
                continue;
            }

            list.add(new DiffUtilEntity(
                    i,
                    "Item " + i,
                    "This item " + i + " content",
                    "06-12")
            );
        }
        return list;
    }
}
